package com.example.consult.controller;

import com.example.consult.model.Employee;
import com.example.consult.model.UserInfo;
import com.example.consult.service.EmployeeService;
import com.example.consult.service.UserService;
import com.example.consult.util.RedisUtil;
import com.example.consult.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/")
public class FirstController {

    HttpSession session ;
    @Autowired
    private UserService userService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    RedisUtil redisUtil;

    @Autowired
    Utils utils;

    @Autowired
    HttpServletRequest request;

    @RequestMapping("Login")
    public String first(){
        session = request.getSession();
        if(session.getAttribute("User")!=null){
            return "Welcome";
        }
        return "Login";
    }
    @RequestMapping("LoginAdmin")
    public String admin(){
        session = request.getSession();
        if(session.getAttribute("Employee")!=null){
            return "AdminView";
        }
        return "LoginAdmin";
    }
    @RequestMapping("Welcome")
    public String welcome(){
        return "Welcome";
    }
    @RequestMapping("WelcomeAdmin")
    public String welcomeAdmin(){
        return "AdminView";
    }

    @RequestMapping("Register")
    public String register(){
        return "Register";
    }

    @RequestMapping("login")
    @ResponseBody
    public String doLogin(@RequestParam String userAccount,String userPwd){
        UserInfo user = userService.doLogin(userAccount,userPwd);
        System.out.println(user);
        if (user!=null){
            session = request.getSession();
            session.setAttribute("User",user);
            return user.toString();
        }else{
            return "ERROR";
        }
    }

    @RequestMapping("loginAdmin")
    @ResponseBody
    public String doLoginAdmin(@RequestParam String userAccount,String userPwd){
        Employee employee = employeeService.doLogin(userAccount,userPwd);
        System.out.println(employee);
        if (employee!=null){
            session = request.getSession();
            //刚上线的员工，没有对应的客户
            session.setAttribute("Employee",employee);
            redisUtil.hset("Employee",employee.getEmployee_id(),"");
            return employee.toString();
        }else{
            return "ERROR";
        }
    }

    @RequestMapping("visitorLogin")
    @ResponseBody
    public String doVisitorLogin(){
        HttpSession session = request.getSession();
        String sessionName="游客"+ utils.getRandom();
        session.setAttribute("Visitor",sessionName);
        return sessionName;
    }

    @RequestMapping("isUsed")
    @ResponseBody
    public boolean isUsed(String account){
        return userService.isUsed(account);
    }

    @RequestMapping("addUser")
    @ResponseBody
    public String addUser(String account,String password){
        UserInfo user = userService.register(account,password);
        HttpSession session = request.getSession();
        session.setAttribute("User",user);
        return user.toString();
    }


    @RequestMapping("AdminView")
    public String adminView(){
        return "AdminView";
    }
    @RequestMapping("MainView")
    public String mainView(){
        return "MainView";
    }


    @RequestMapping("Exit")
    public String exit(){
        HttpSession session = request.getSession();
        session.removeAttribute("User");
        return "/Login";
    }
    @RequestMapping("ExitAdmin")
    public String exitAdmin(){
        HttpSession session = request.getSession();
        session.removeAttribute("Employee");
        return "/LoginAdmin";
    }
}
